home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_4_6.zip / CUSTCNTL.C < prev    next >
C/C++ Source or Header  |  1991-02-21  |  8KB  |  265 lines

  1. /****************************************************************************
  2. Module name: CustCntl.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #undef NOCOLOR
  8. #undef NOCTLMGR
  9. #undef NOGDI
  10. #undef NOKERNEL
  11. #undef NOLSTRING
  12. #undef NOMB
  13. #undef NOMENUS
  14. #undef NOMINMAX
  15. #undef NOMSG
  16. #undef NORASTEROPS
  17. #undef NOSHOWWINDOW
  18. #undef NOSYSMETRICS
  19. #undef NOUSER
  20. #undef NOWINOFFSETS
  21. #undef NOWINMESSAGES
  22. #undef NOWINSTYLES
  23. #include <windows.h>
  24.  
  25. #include "custcntl.h"
  26.  
  27. char _szAppName[] = "CustCntl";
  28.  
  29. HANDLE _hInstance = NULL;  // Our instance handle
  30.  
  31. #define IDM_DEMO        (0x0110)   // Must be < 0xF000 (GetSystemMenu)
  32. #define IDM_ABOUT       (0x0120)   // Must be < 0xF000
  33.  
  34. BOOL NEAR PASCAL RegisterAppWndClass (HANDLE hInstance);
  35. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam);
  36. BOOL FAR PASCAL CustCntlDlgProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam);
  37. LONG FAR PASCAL AppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
  38.  
  39.  
  40. // ***************************************************************************
  41. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {
  42.    MSG msg;
  43.    HWND hWnd;
  44.    HMENU hMenu;
  45.    HANDLE hLibMeter, hLibSpin;
  46.  
  47.    _hInstance = hInstance;
  48.  
  49.    if (hPrevInstance == NULL)
  50.       if (!RegisterAppWndClass(hInstance))
  51.          return(0);
  52.  
  53.    hWnd = CreateWindow(_szAppName, _szAppName,
  54.       WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
  55.       CW_USEDEFAULT, nCmdShow, CW_USEDEFAULT, CW_USEDEFAULT,
  56.       NULL, NULL, hInstance, 0);
  57.    if (hWnd == NULL) return(0);
  58.  
  59.    // Get handle to application's System Menu.
  60.    hMenu = GetSystemMenu(hWnd, 0);
  61.  
  62.    // Append separator bar & two options.
  63.    AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
  64.    AppendMenu(hMenu, MF_STRING,
  65.       IDM_DEMO, "&Custom control demo...");
  66.    AppendMenu(hMenu, MF_STRING, IDM_ABOUT, "A&bout...");
  67.    DrawMenuBar(hWnd);
  68.  
  69.    hLibMeter = LoadLibrary("METER.DLL");
  70.    if (hLibMeter < 32) return(0);
  71.  
  72.    hLibSpin  = LoadLibrary("SPIN.DLL");
  73.    if (hLibSpin < 32) {
  74.       FreeLibrary(hLibMeter);
  75.       return(0);
  76.    }
  77.  
  78.    while (GetMessage(&msg, NULL, 0, 0)) {
  79.       TranslateMessage(&msg);
  80.       DispatchMessage(&msg);
  81.    }
  82.  
  83.    FreeLibrary(hLibMeter);
  84.    FreeLibrary(hLibSpin);
  85.    return(0);
  86. }
  87.  
  88.  
  89.  
  90. // ***************************************************************************
  91. // This function registers the application's main window.
  92.  
  93. BOOL NEAR PASCAL RegisterAppWndClass (HANDLE hInstance) {
  94.    WNDCLASS WndClass;
  95.  
  96.    WndClass.style         = 0;
  97.    WndClass.lpfnWndProc   = AppWndProc;
  98.    WndClass.cbClsExtra    = 0;
  99.    WndClass.cbWndExtra    = 0;
  100.    WndClass.hInstance     = hInstance;
  101.    WndClass.hIcon         = LoadIcon(hInstance, _szAppName);
  102.    WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  103.    WndClass.hbrBackground = COLOR_WINDOW + 1;
  104.    WndClass.lpszMenuName  = NULL;
  105.    WndClass.lpszClassName = _szAppName;
  106.    return(RegisterClass(&WndClass));
  107. }
  108.  
  109.  
  110. // ***************************************************************************
  111. // This function processes all messages sent to the modeless dialog box.
  112.  
  113. BOOL FAR PASCAL CustCntlDlgProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  114.    BOOL fProcessed = TRUE;
  115.    int x;
  116.  
  117.    switch (wMsg) {
  118.       case WM_INITDIALOG:
  119.          // Tell Meter control that the job consists of 25 parts.
  120.          SendDlgItemMessage(hDlg, ID_METER, MM_SETPARTSINJOB, 25, 0);
  121.  
  122.          // Tell Meter control that the zero parts of the job are complete.
  123.          SendDlgItemMessage(hDlg, ID_METER, MM_SETPARTSCOMPLETE, 0, 0);
  124.  
  125.          // Tell Spin Button that the valid range is 0 to 25.
  126.          SendDlgItemMessage(hDlg, ID_SPIN, SPNM_SETRANGE, 0,
  127.             MAKELONG(0, 25));
  128.  
  129.          // Tell Spin Button that the current value is 0.
  130.          SendDlgItemMessage(hDlg, ID_SPIN, SPNM_SETCRNTVALUE, 0, 0);
  131.          break;
  132.  
  133.       case WM_COMMAND:
  134.          switch (wParam) {
  135.             case ID_SPIN:
  136.                switch (HIWORD(lParam)) {
  137.                   case SPNN_VALUECHANGE:
  138.                      // User has changed the current value of the Spin Button.
  139.  
  140.                      // Request the current value from the Spin Button.
  141.                      x = (int) SendMessage(LOWORD(lParam), SPNM_GETCRNTVALUE, 0, 0);
  142.  
  143.                      // Tells the Meter control the new number of parts that 
  144.                      // are complete.  This is an example of the Meter control
  145.                      // allows for the "parts-complete" value to go both 
  146.                      // up and down.
  147.                      SendDlgItemMessage(hDlg, ID_METER, MM_SETPARTSCOMPLETE,
  148.                         x, 0);
  149.  
  150.                      // Update the static window to reflect the current value
  151.                      // in the Spin Button.
  152.                      SetDlgItemInt(hDlg, ID_SPINVALUE, x, FALSE);
  153.                      break;
  154.                }
  155.                break;
  156.  
  157.             case IDOK:
  158.             case IDCANCEL:
  159.                EndDialog(hDlg, wParam);
  160.                break;
  161.  
  162.             default: break;
  163.          }
  164.          break;
  165.  
  166.       default:
  167.          fProcessed = FALSE;
  168.          break;
  169.    }
  170.  
  171.    return(fProcessed);
  172. }
  173.  
  174.  
  175. // ***************************************************************************
  176. // This function processes all messages sent to the app's main window.
  177.  
  178. LONG FAR PASCAL AppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
  179.    BOOL fCallDefProc = FALSE;
  180.    LONG lResult = 0;
  181.    FARPROC fpProc;
  182.  
  183.    switch (wMsg) {
  184.       case WM_DESTROY:
  185.          PostQuitMessage(0);
  186.          break;
  187.  
  188.       case WM_SYSCOMMAND:
  189.          // Any menu option selected from CustCntl's System Menu.
  190.  
  191.          // Any option's that we appended to System menu should be
  192.          // processed by CustCntl and NOT passed to DefWindowProc.
  193.  
  194.          switch (wParam & 0xfff0) {
  195.  
  196.          case IDM_ABOUT:
  197.             // Display about box
  198.             fpProc = MakeProcInstance(AboutProc, _hInstance);
  199.             DialogBox(_hInstance, "About", hWnd, fpProc);
  200.             FreeProcInstance(fpProc);
  201.             break;
  202.  
  203.          case IDM_DEMO:
  204.             // Display about box
  205.             fpProc = MakeProcInstance(CustCntlDlgProc, _hInstance);
  206.             DialogBox(_hInstance, "CustCntl", hWnd, fpProc);
  207.             FreeProcInstance(fpProc);
  208.             break;
  209.  
  210.          default:
  211.             // Any option's that we do not process should be 
  212.             // passed to DefWindowProc.
  213.             fCallDefProc = TRUE;
  214.             break;
  215.          }
  216.  
  217.          break;
  218.  
  219.  
  220.       default:
  221.          fCallDefProc = TRUE; break;
  222.    }
  223.  
  224.    if (fCallDefProc)
  225.       lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
  226.  
  227.    return(lResult);
  228. }
  229.  
  230.  
  231.  
  232. // ***************************************************************************
  233. // This function processess all messages sent to the About dialog box.
  234.  
  235. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  236.    char szBuffer[100];
  237.    BOOL fProcessed = TRUE;
  238.  
  239.    switch (wMsg) {
  240.  
  241.       case WM_INITDIALOG:
  242.          // Set version static window to have date and time of compilation.
  243.          wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  244.          SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  245.          break;
  246.  
  247.       case WM_COMMAND:
  248.          switch (wParam) {
  249.             case IDOK:
  250.             case IDCANCEL:
  251.                if (HIWORD(lParam) == BN_CLICKED)
  252.                   EndDialog(hDlg, wParam);
  253.                break;
  254.  
  255.             default:
  256.                break;
  257.          }
  258.          break;
  259.  
  260.       default:
  261.          fProcessed = FALSE; break;
  262.    }
  263.    return(fProcessed);
  264. }
  265.